package com.ck.lock.reentrantlock;

import com.ck.lock.common.Counter;


/**
 * @author ck
 * @date 2023/7/3 16:57
 */
public class ReentrantLockDemoTest {

    /**
     * ReentrantLock(重入锁) 可公平 可不公平
     * 特性：效率较高，灵活，可感知获取锁，可公平可非公平，可中断
     * 缺点：需在finally释放锁
     */


    public static void main(String[] args) throws InterruptedException {
        System.out.println("目标结果是：100000");
        Counter counter = new Counter();

        //线程t1让count成员自增到5w
        Thread t1=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increaseForReentrantLock();
            }

        });
        t1.start();

        //线程t2让count成员自增到5w
        Thread t2=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increaseForReentrantLock();
            }
        });
        t2.start();

        //等待两个线程都结束
        t1.join();
        t2.join();
        System.out.println("多线程处理下："+counter.getCount());
    }

    /**
     目标结果是：100000
     多线程处理下：100000
     */
}
